"""
_*_ : utf-8 _*_ 
@Time: 2023/10/31 22:00
@Author: LWK
@File: 061_ajax_豆瓣电影
@Project: demo01
"""

import urllib.request
import urllib.parse


def creat_request(page_num, page_size):
    url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76'
    }
    params = {
        'cname': '上海',
        'pid': '',
        'pageIndex': page_num,
        'pageSize': page_size
    }
    data = urllib.parse.urlencode(params).encode('utf-8')
    return urllib.request.Request(url, data, headers)


def get_content(req):
    res = urllib.request.urlopen(req)
    content = res.read().decode('utf-8')
    return content


def down_load(content, page):
    with open('../temp/kfc_%s.json' % str(page), 'w', encoding='utf-8') as fp:
        fp.write(content)


if __name__ == '__main__':
    start_page = int(input('请输入起始的页码：'))
    end_page = int(input('请输入结束的页码：'))

    for page_item in range(start_page, end_page + 1):
        req_init = creat_request(page_item, 10)
        content_init = get_content(req_init)
        down_load(content_init, page_item)
